package snmp_trap;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Vector;

import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.MessageDispatcherImpl;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.mp.MPv1;
import org.snmp4j.mp.MPv2c;
import org.snmp4j.mp.MPv3;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.MultiThreadedMessageDispatcher;
import org.snmp4j.util.ThreadPool;

public class TrapReceiver implements CommandResponder{

	private static int count = 0;
	private Snmp snmp;
	private Address listenAddress;
	private ThreadPool threadPool;
	private MultiThreadedMessageDispatcher dispathcer;
	
	public void init() throws IOException {
		threadPool = ThreadPool.create("thread", 2);
		dispathcer = new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl());
		listenAddress = GenericAddress.parse(System.getProperty("snmp4j.listenAddress", "udp:127.0.0.1/162"));
		TransportMapping transport = new DefaultUdpTransportMapping((UdpAddress) listenAddress);
		snmp = new Snmp(dispathcer, transport);
		snmp.getMessageDispatcher().addMessageProcessingModel(new MPv1());
		snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c());
		snmp.getMessageDispatcher().addMessageProcessingModel(new MPv3());
		USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
		SecurityModels.getInstance().addSecurityModel(usm);
		snmp.listen();
	}
	
	public void run() {
		try {
			init();
			snmp.addCommandResponder(this);
			System.out.println("监听Trap启动！");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void processPdu(CommandResponderEvent commandresponderevent) {
		count++;
		System.out.println("-----------" + count + "-------------");
		System.out.println("Event === " + commandresponderevent.toString());
		PDU pdu = commandresponderevent.getPDU();
		Vector vecVBs = pdu.getVariableBindings();
		for (int i = 0; i < vecVBs.size(); i++) {
			VariableBinding vecVB = (VariableBinding) vecVBs.get(i);
            String attributeValue = null;
            if (vecVB.getVariable() instanceof OctetString ){
            	
                byte[] bs = ((OctetString)vecVB.getVariable()).getValue();
                attributeValue = new String (bs);
            } else{
            	attributeValue =  vecVB.getVariable().toString().trim();
            }
			System.out.println(vecVB.getOid() + ":" + attributeValue);
		}
	}

	public static void main(String[] args) {
		TrapReceiver trapReceiver = new TrapReceiver();
		trapReceiver.run();
	}
}
